package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.Enterprise;
import com.innovations.inn.domain.response.ResponseEnterpriseInfo;
import com.innovations.inn.domain.request.RequestEnterpriseInfoAll;
import com.innovations.inn.domain.response.ResponseEnterpriseInfoAll;
import com.innovations.inn.service.IEnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业信息Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "企业信息")
@RestController
@RequestMapping("/inn/enterprise")
public class EnterpriseController extends BaseController {
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询企业信息列表
     */
    @ApiOperation("查询企业信息列表")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(Enterprise enterprise) {
        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 查询企业详细信息列表
     */
    @ApiOperation("查询企业详细信息列表")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list/info")
    public TableDataInfo listInfo(Enterprise enterprise) {
        startPage();
        List<ResponseEnterpriseInfo> list = enterpriseService.selectEnterpriseListInfo(enterprise);
        return getDataTable(list);
    }

    /**
     * 查询企业详细信息列表
     */
    @ApiOperation("查询企业及资质详细信息列表")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list/info/all")
    public TableDataInfo listInfoAll(Enterprise enterprise) {
        startPage();
        List<ResponseEnterpriseInfoAll> list = enterpriseService.selectEnterpriseListInfoAll(enterprise);
        return getDataTable(list);
    }

    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('inn:enterprise:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Enterprise enterprise) {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        util.exportExcel(response, list, "企业信息数据");
    }

    /**
     * 获取企业信息详细信息
     */
    @ApiOperation("获取企业信息详细信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:query')")
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId) {
        return success(enterpriseService.selectEnterpriseByEnterpriseId(enterpriseId));
    }

    /**
     * 新增企业信息
     */
    @ApiOperation("新增企业信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Enterprise enterprise) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseService.completeData(enterprise, userId, 1);
        return toAjax(enterpriseService.insertEnterprise(enterprise));
    }

    /**
     * 新增企业及资质信息
     */
    @ApiOperation("新增企业及资质信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping("/info")
    public AjaxResult addAll(HttpServletRequest request, @RequestBody RequestEnterpriseInfoAll enterprise) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterprise.setUserId(userId);
        return toAjax(enterpriseService.insertEnterpriseAll(enterprise));
    }

    /**
     * 修改企业信息
     */
    @ApiOperation("修改企业信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody Enterprise enterprise) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseService.completeData(enterprise, userId, 2);
        return toAjax(enterpriseService.updateEnterprise(enterprise));
    }

    /**
     * 删除企业信息
     */
    @ApiOperation("删除企业信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseIds) {
        return toAjax(enterpriseService.deleteEnterpriseByEnterpriseIds(enterpriseIds));
    }

}
